package ebj.common.queue.request.service;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import ebj.common.queue.meta.Message;
import ebj.common.queue.request.queue.QueryExecuteThreadPool;
import ebj.common.queue.request.queue.QueryQueue;

public class Report implements HttpHandler {
    private static Logger logger = Logger.getLogger(Report.class);
    final static String mimeType = "text/json";

    public void handle(HttpExchange httpExchange) throws IOException {
        /*
         *  http://localhost:9099/report?report=ok
         *  上述查询的 query = "report=ok"
         */
        String query = httpExchange.getRequestURI().getQuery();
        QueryQueue.addQuery(URLDecoder.decode(query, "UTF-8")); // 查询放入队列
        // QueryExecuteThreadPool.initCustomerPool();
        logger.info("query: " + query);
        System.out.println("请求方式： " + httpExchange.getRequestMethod());
        
        System.out.println(query);
        // Message message = new Message(MSGCode.NDP_MSG_TYPE_INFO,
        // MSGCode.NDP_MSG_CODE_SERVER_OK, MSGCode.NDP_MSG_SERVER_OK);
        // String response = XMLUtils.msg2XML(message);
        String response = new Gson().toJson(new Message("result", "200", "Report results."));

//        Headers responseHeaders = httpExchange.getResponseHeaders();
//        responseHeaders.set("Content-Type", mimeType);

        httpExchange.sendResponseHeaders(200, response.length());
        OutputStream outputStream = httpExchange.getResponseBody();
        outputStream.write(response.getBytes("UTF-8"));
        outputStream.flush();
        outputStream.close();
    }

}
